home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWString / Include / FWCharac.h next >
Encoding:
C/C++ Source or Header  |  1994-04-21  |  7.6 KB  |  203 lines  |  [TEXT/MPS ]

  1. #ifndef FWCHARAC_H
  2. #define FWCHARAC_H
  3. //========================================================================================
  4. //
  5. //    File:                FWCharac.h
  6. //    Release Version:    $ 1.0d1 $
  7. //
  8. //    Creation Date:        3/28/94
  9. //
  10. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //========================================================================================
  13.  
  14. #ifndef FWSTDDEF_H
  15. #include "FWStdDef.h"
  16. #endif
  17.  
  18. #ifndef FWPRIMEM_H
  19. #include "FWPriMem.h"
  20. #endif
  21.  
  22. // Native Character sets are determined by two parameters: 1) maximum character size,
  23. // and 2) fixed vs. variable width characters.  If characters are fixed width, then
  24. // all characters are maximum character size in width.
  25. //
  26. // This strings component also uses a "median" character size value when dealing with
  27. // variable width characters.  In all variable width systems we're aware of, the median
  28. // value should be 2 bytes.  This median value is used in one place: computing
  29. // the capacity of a bounded string from the template capacity parameter.
  30. //
  31. // Note that all strings are terminated with a "NUL" character, which is a FW_Char(0).
  32. // This may be more zero bytes than necessary in some characters sets, but it's a simple
  33. // and safe rule to follow.
  34.  
  35. #if   defined FW_BYTE_CHARACTERS
  36.     typedef char FW_Char;
  37. #elif defined FW_SHORT_CHARACTERS
  38.     typedef short FW_Char;
  39. #elif defined FW_LONG_CHARACTERS
  40.     typedef long FW_Char;
  41. #else
  42.     typedef char FW_Char;    // default to byte wide
  43. #define FW_BYTE_CHARACTERS
  44. #endif
  45.  
  46. typedef unsigned char    FW_PascalChar;
  47.  
  48. typedef unsigned char FW_Byte;
  49. typedef long FW_CharacterCount;
  50. typedef long FW_CharacterPosition;
  51.  
  52. typedef long FW_ByteCount;
  53. typedef long FW_BytePosition;
  54.  
  55. enum {FW_kNulCharacter=0};
  56.  
  57. #ifdef  FW_VARIABLE_WIDTH_CHARACTERS
  58.     enum {FW_kMedianCharacterSize=2};
  59. #else
  60.     enum {FW_kMedianCharacterSize=sizeof(FW_Char)};
  61. #endif
  62.  
  63. //----------------------------------------------------------------------------------------
  64. //    FW_StringLength
  65. //----------------------------------------------------------------------------------------
  66.  
  67. FW_CharacterCount FW_StringLength(const FW_Char * string);
  68.  
  69. //----------------------------------------------------------------------------------------
  70. //    FW_BlockMove
  71. //----------------------------------------------------------------------------------------
  72.  
  73. void FW_BlockMove(const FW_Byte *source, FW_Byte *destination, FW_ByteCount numberBytes);
  74.  
  75. //========================================================================================
  76. // ----- Utility functions for byte <==> character mapping
  77. //========================================================================================
  78.     
  79. //----------------------------------------------------------------------------------------
  80. //    FW_BytesInString
  81. //----------------------------------------------------------------------------------------
  82.  
  83. FW_ByteCount FW_BytesInString(const void* string, FW_CharacterCount length);
  84.     // Returns number of bytes in string.
  85.     // For fixed-width character sets this function does not examine the string,
  86.     //  so it is very fast.
  87.     // For variable-width character sets this function searches the string,
  88.     //  so it might be slow!
  89.     
  90. //----------------------------------------------------------------------------------------
  91. //    FW_BytePositionInString
  92. //----------------------------------------------------------------------------------------
  93.  
  94. FW_Byte* FW_BytePositionInString(const void* string, FW_CharacterCount position);
  95.     // Returns pointer to given character in string.
  96.     // Uses FW_BytesInString(), so same notes apply.
  97.     
  98. //----------------------------------------------------------------------------------------
  99. //    FW_CharactersInBlock
  100. //----------------------------------------------------------------------------------------
  101.  
  102. FW_CharacterCount FW_CharactersInBlock(const void *block, FW_ByteCount length);
  103.     // Returns number of characters in block of bytes.
  104.     // For fixed-width character sets this function does not examine the string,
  105.     //  so it is very fast.
  106.     // For variable-width character sets this function searches the string,
  107.     //  so it might be slow!
  108.  
  109. //----------------------------------------------------------------------------------------
  110. //    FW_PreviousCharacter
  111. //----------------------------------------------------------------------------------------
  112.  
  113. FW_Byte* FW_PreviousCharacter(FW_Byte* string, FW_Byte* current, short delta=1);
  114.     // A potentially very slow routine for backing up through a string.
  115.     // string and current must be pointers into the same string, each
  116.     // pointing at the first byte of a character, and string < current.
  117.     // The function result is a pointer to the character delta positions before the character 
  118.     // at current, i.e. current decremented by the number of bytes in the delta previous characters.
  119.     
  120. //----------------------------------------------------------------------------------------
  121. //    FW_PreviousCharacter
  122. //----------------------------------------------------------------------------------------
  123.  
  124. const FW_Byte* FW_PreviousCharacter(const FW_Byte* string, const FW_Byte* current, short delta=1);
  125.     // The const version of the above function.
  126.     
  127. //----------------------------------------------------------------------------------------
  128. //    FW_BlockMove
  129. //----------------------------------------------------------------------------------------
  130.  
  131. inline void FW_BlockMove(const FW_Byte *source, FW_Byte *destination, FW_ByteCount numberItems)
  132. {
  133.     FW_PrimitiveCopyMemory(source, destination, numberItems);
  134. }
  135.  
  136. //----------------------------------------------------------------------------------------
  137. //    FW_BytesInString
  138. //----------------------------------------------------------------------------------------
  139.  
  140. inline FW_ByteCount FW_BytesInString(const void* string, FW_CharacterCount length)
  141. {
  142. #ifdef  FW_VARIABLE_WIDTH_CHARACTERS
  143.     // ???JEL - Need to call operating system here
  144.     return NotYetImplemented();
  145. #else
  146.     return length*sizeof(FW_Char);
  147. #endif
  148. }
  149.         
  150. //----------------------------------------------------------------------------------------
  151. //    FW_CharactersInBlock
  152. //----------------------------------------------------------------------------------------
  153.  
  154. inline FW_CharacterCount FW_CharactersInBlock(const void *block, FW_ByteCount length)
  155. {
  156. #ifdef  FW_VARIABLE_WIDTH_CHARACTERS
  157.     // ???JEL - Need to call operating system here
  158.     return NotYetImplemented();
  159. #else
  160.     return length/sizeof(FW_Char);
  161. #endif
  162. }
  163.         
  164. //----------------------------------------------------------------------------------------
  165. //    FW_PreviousCharacter
  166. //----------------------------------------------------------------------------------------
  167.  
  168. inline FW_Byte* FW_PreviousCharacter(FW_Byte* string, FW_Byte* current, short delta)
  169. {
  170. #ifdef  FW_VARIABLE_WIDTH_CHARACTERS
  171.     return string + 
  172.         FW_BytesInString(string, FW_CharactersInBlock(string, current-string)-delta);
  173. #else
  174.     return current - sizeof(FW_Char)*delta;
  175. #endif
  176. }
  177.  
  178. //----------------------------------------------------------------------------------------
  179. //    FW_PreviousCharacter
  180. //----------------------------------------------------------------------------------------
  181.  
  182. inline const FW_Byte* FW_PreviousCharacter(const FW_Byte* string, const FW_Byte* current, short delta)
  183. {
  184. #ifdef  FW_VARIABLE_WIDTH_CHARACTERS
  185.     return string + 
  186.         FW_BytesInString(string, FW_CharactersInBlock(string, current-string)-delta);
  187. #else
  188.     return current - sizeof(FW_Char)*delta;
  189. #endif
  190. }
  191.  
  192. //----------------------------------------------------------------------------------------
  193. //    FW_BytePositionInString
  194. //----------------------------------------------------------------------------------------
  195.  
  196. inline FW_Byte* FW_BytePositionInString(const void* string, FW_CharacterCount position)
  197. {
  198.     return (FW_Byte*) string + FW_BytesInString(string, position);
  199. }
  200.         
  201. #endif
  202.  
  203.